<?php 
	session_start();
	if(file_exists('database.php')) {
		require_once 'database.php';
	}
  if(!isset($_SESSION['isLogged'])) {
    header("Location: index.php");
    exit;
  }
 ?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
   "http://www.w3.org/TR/html4/strict.dtd">
<HTML>
   <HEAD>
      <TITLE>Edit</TITLE>
      <meta charset="utf8"/>
   </HEAD>
   <BODY>
      <?php
        
         if(!isset($_GET['row'])) {
            echo "
               <form method=\"POST\" enctype=\"multipart/form-data\">
                  <div>Model:<input type=\"text\" name=\"model\"/></div>
                  <div>Price:<input type=\"text\" name=\"price\"/></div>
                  <div>Image:<input type=\"file\" name=\"file\"><br></div>
                  <input type=\"submit\" value=\"Запази\"/>
               </form>
            ";
            if($_POST) {
               $model = trim($_POST['model']);
               $price = trim($_POST['price']);
               $filePath = "images/".$_FILES["file"]["name"];
                if (file_exists("upload\\" . $_FILES["file"]["name"]))
                   {
                      echo $_FILES["file"]["name"] . " already exists. ";
                   }
               else
                  {
                     $sql = "INSERT INTO traktor(model, image, price) VALUES('$model', '$filePath', '$price')";
                     mysqli_query($db, $sql);
                     move_uploaded_file($_FILES["file"]["tmp_name"],
                     "images/" . $_FILES["file"]["name"]);
                     echo "Stored in: " . "images\\" . $_FILES["file"]["name"];
                     header("Location: traktorSystem.php");
                     }
                }
             } else {
                $id = $_GET['row'];
                $sql = "SELECT model, price from traktor where traktor_id = $id";
                $row = mysqli_query($db, $sql)->fetch_assoc();
                $model = $row['model'];
                $price = $row['price'];
               echo "
               <form method=\"POST\" enctype=\"multipart/form-data\">
                  <div>Model:<input type=\"text\" name=\"model\" value=\"$model\"/></div>
                  <div>Price:<input type=\"text\" name=\"price\" value=\"$price\"/></div>
                  <div>Image:<input type=\"file\" name=\"file\"><br></div>
                  <input type=\"submit\" value=\"Добави трактор\"/>
               </form>
            ";
               if($_POST) {
               $model = trim($_POST['model']);
               $price = trim($_POST['price']);
               $filePath = "images/".$_FILES["file"]["name"];
                if (file_exists("upload\\" . $_FILES["file"]["name"]))
                   {
                      echo $_FILES["file"]["name"] . " already exists. ";
                   }
               else
                  {
                     $sql = "UPDATE traktor SET model = '$model', image = '$filePath', price = '$price' where traktor_id = $id";
                     mysqli_query($db, $sql);
                     move_uploaded_file($_FILES["file"]["tmp_name"],
                     "images/" . $_FILES["file"]["name"]);
                     echo "Stored in: " . "images\\" . $_FILES["file"]["name"];
                     header("Location: traktorSystem.php");
                     }
                }
             }
      ?>
   </BODY>
</HTML>